package controles;


import java.awt.Component;
import java.util.* ;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.* ;

public class GenericTableModel extends AbstractTableModel
{

	// Dimension de la table

	public int iNbLine ;
	public int iNbCol ;

	private boolean 	bEditable ;

	// Entete de colonne

	private Vector oVectColHeader ;
	
	// Vecteur des lignes : chaque ligne est constitu�e d'un vecteur de colonnes
	
	private Vector oVectLine  ;

	// Constructeur public

	public GenericTableModel (String _tzColHeader [])
	{
		super () ;
		oVectLine = new Vector () ;
		oVectColHeader = new Vector () ;
		for (int iIndex = 0 ; iIndex < _tzColHeader.length ; iIndex++)
		{
			oVectColHeader.addElement (_tzColHeader[iIndex]) ;
		}
		iNbLine = 0 ;
		bEditable = true ;
		iNbCol = _tzColHeader.length ;
	}

	public void setEditable (boolean _bEdit)
	{
		bEditable = _bEdit ;
	}

	// Suppression de toutes les lignes

	public void removeAllRows ()
	{
		iNbLine = 0 ;
		oVectLine.removeAllElements () ;
	}
	
	// Ajout d'une nouvelle ligne

	public void addNewRow ()
	{
		iNbLine++ ;
		Vector oVectNewLine = new Vector () ;
		for (int iIndex = 0 ; iIndex < iNbCol ; iIndex++)
		{
			oVectNewLine.addElement (new java.lang.Object ());
		}
		oVectLine.addElement (oVectNewLine) ;
	}

	// M�thodes obligatoires (h�rit�es de AbstractTableModel)

	public int getColumnCount () 
	{
		return iNbCol ;
	}

	public int getRowCount ()
	{
		return iNbLine ;
	}

	public Object getValueAt (int _iLig, int _iCol)
	{
		Object oObj = null ;
		if (_iLig < iNbLine)
		{
			Vector oLine = (Vector) oVectLine.elementAt (_iLig) ;
			oObj = oLine.elementAt (_iCol) ;
		}
		if (oObj == null )
		{
			oObj = "";
		}
		return oObj ;
	}

	/* M�thodes red�finies */

	public String getColumnName (int _iCol)
	{
		return (String) oVectColHeader.elementAt (_iCol) ;
	} 

	public Class getColumnClass (int _iCol)
	{
		return getValueAt (0, _iCol).getClass () ;
	}
			
	public boolean isCellEditable (int _iLig, int _iCol)
	{
		return bEditable ;
	}

	public void setValueAt (Object _oObjectValue, int _iLig, int _iCol)
	{
		Vector oLine = (Vector) oVectLine.elementAt (_iLig) ;
		oLine.setElementAt (_oObjectValue, _iCol) ;
		oVectLine.setElementAt (oLine, _iLig) ;
		fireTableDataChanged () ;
	}

	public void dump ()
	{
		for (int iIndexLig = 0 ; iIndexLig < iNbLine ; iIndexLig++)
		{
			for (int iIndexCol = 0 ; iIndexCol < iNbCol ; iIndexCol++)
			{
				String zContent = (String) getValueAt (iIndexLig, iIndexCol) ;
				System.out.print ("[" +  iIndexLig + ";" +  iIndexCol + "] : " + zContent + "\t") ;
			}
			System.out.println ("") ;
		}
	}
	
	

}

